package lei.m_线程池_事物;

/**
 * @className: SimpleSingleThreadExample
 * @author: xl
 * @date: 2025/7/26 16:22
 * @Version: 1.0
 * @description:
 */

public class SimpleSingleThreadExample {

    public static void main(String[] args) {
        System.out.println("主程序开始");

        // 直接创建并启动线程
        Thread thread = new Thread(() -> {
            System.out.println("线程开始执行: " + Thread.currentThread().getName());

            // 执行业务逻辑
            for (int i = 1; i <= 5; i++) {
                System.out.println("执行步骤 " + i);
                try {
                    Thread.sleep(1000); // 每步间隔1秒
                } catch (InterruptedException e) {
                    System.err.println("线程被中断");
                    Thread.currentThread().interrupt();
                    return;
                }
            }

            System.out.println("线程执行完成");
        }, "MySingleThread");

        // 启动线程
        thread.start();

        // 等待线程完成
        try {
            thread.join();
            System.out.println("主程序结束");
        } catch (InterruptedException e) {
            System.err.println("主线程等待被中断");
            Thread.currentThread().interrupt();
        }
    }
}

